new Vue({
    el:'#main',
    data:()=>({
        type:'',
        dataval:{
            buy_persion:[],
            port:[],
            procat:[]
        },//基础数据
        prolist:[],//产品
        company:[],//公司
        history:[],//采购历史
        prodata:null,//产品资料
        prochar:{},//产品资料
        norm:null,//装箱规格
        pro_item:[],//产品配件
        prodesc_cat:[],
        data:{
            buy_id:0,
            delivery_at:'',
            port_id:0,
            company_id:0,
            pro_id:0,
            prochar_id:0,
            qty:0,
            price:'',//单价
            proval:''
        }
    }),
    methods:{
        getdata(){
            var data={}
            if(this.type=='add'){
                var url='/admin/buy/plan_addnew'//添加请求的地址
            }else{
                var url='/admin/buy/plan_editnew?id='+$('#plan').val() //编辑请求的地址
            }

            $.ajax({
                url,
                type:'post',
                data,
                success:(data)=>{
                    data = JSON.parse(data)
                    if(this.type=='add'){
                        this.dataval=data
                    }else{
                        for (var key in data.planlist) {
                            if (this.data.hasOwnProperty(key)) {
                                this.data[key]=data.planlist[key]
                            }
                        }
                        for (var key in data) {
                            if (this.hasOwnProperty(key)) {
                                this[key]=data[key]
                            }
                            if (this.dataval.hasOwnProperty(key)) {
                                this.dataval[key]=data[key]
                            }
                        }
                        this.data.proval=this.data.prochar_id+'-'+this.data.pro_id
                        this.prodata=data.proAndProchar.proList
                        this.prochar=data.proAndProchar.prochar
                    }
                }
            })
        },
        sqlpro(){
            $.ajax({
                url:'/admin/buyapi/searchData',
                type:'post',
                data:{
                    searchText:this.$refs.searchText.value,
                    proclass:this.$refs.procat.value
                },
                success:(data)=>{
                    data = JSON.parse(data)
                    this.prolist=data
                }
            }) 
        },
        getcompany(){
            $.ajax({
                url:'/admin/buyapi/searchCompany',
                type:'post',
                data:{
                    searchCompany:this.$refs.company.value
                },
                success:(data)=>{
                    data = JSON.parse(data)
                    this.company=data
                }
            }) 
        },
        changepro(){
            if(this.$refs.selpro.value!=0){
                this.data.pro_id=this.$refs.selpro.value.split('-')[1]
                this.data.prochar_id=this.$refs.selpro.value.split('-')[0]
                this.data.proval=this.data.prochar_id+'-'+this.data.pro_id
                $.ajax({
                    url:'/admin/buyapi/getProList',
                    type:'post',
                    data:{
                        pro_id:this.data.pro_id,
                        prochar_id: this.data.prochar_id
                    },
                    success:(data)=>{
                        data = JSON.parse(data)
                        this.history=data.history
                        this.data.price=data.price
                        this.prodata=data.proAndProchar.proList
                        this.prochar=data.proAndProchar.prochar
                        this.norm=data.norm
                        this.pro_item=data.pro_item
                        this.prodesc_cat=data.prodesc_cat
                    }
                })
            }else{
                this.data.proval=0
            }
        },
        savefun(is_back){
            if(this.data.proval){
                if(this.type=='add'){
                    var url='/admin/buy/plan_addnew?request=insert'//添加请求的地址
                }else{
                    var url='/admin/buy/plan_editnew?request=update&id='+$('#plan').val() //编辑请求的地址
                }
                this.data.delivery_at=$('#dateval').val()
                this.pro_item.map(element => {
                    element.is_public=element.is_public ? 1 : 0
                });
                var data={
                    data:this.data,
                    sum:{},
                    norm:this.norm,
                    pro_item:this.pro_item,
                    prodesc_cat: this.prodesc_cat
                }
                data.sum={
                    sumnubval:this.sumnub,
                    sumkgsval:this.sumkgs,
                    sumtgval:this.sumtg,
                    summnval:this.summn
                }
                $.ajax({
                    url,
                    type:'post',
                    data,
                    success:(data)=>{
                        data = JSON.parse(data)
                        change_mes_type('.alert',data.status)
                        $('.alert').show()
                        $('.alert p').html(data.msg)

                        if(data.status==1 && is_back){
                            setTimeout(()=>{
                                location.href="/admin/buy/plan"
                            },500)
                        }
                    }
                })    
            }else{
                change_mes_type('.alert',2)
                $('.alert').show()
                $('.alert p').html(''+langcon.please_select+'')
            }
        }
    },
    mounted(){
        this.type=this.$refs.type.dataset.type//判断添加还是编辑
        this.getdata()
        var date=new Date()
        var month=date.getMonth()
        var year = date.getFullYear()
        if(month+2>12){
            year +=1 
            month=1
        }else{
            month+=2
        }
        this.$refs.date.value=year+'-'+month+'-'+date.getDate()//默认设置交货时间为一个月后
    },
    computed:{
        sumnub:function(){    //合计数量
            return this.data.qty*this.norm.qty
        },
        sumkgs:function(){   //合计重量
            return Math.floor(this.norm.ctn_kgs*this.norm.qty*this.data.qty*100)/100
        },
        sumtg:function(){   //合计体积
            return this.data.qty*this.norm.ctn_m3*this.norm.qty
        },
        summn:function(){   //合计金额
            return Math.floor(this.data.qty*this.data.price*this.norm.qty*100)/100
        },
        numsum:function(){
            return function(num,index){
                this.pro_item[index].qty=Math.ceil(this.data.qty*this.norm.qty*num)
                return Math.ceil(this.data.qty*this.norm.qty*num)
            }
        }
    }
})